package org.dval.core;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.*;
import java.lang.reflect.Field;
import java.util.*;

/**
 * 导入cvs工具类
 * @author cuin
 */
public class ImportCvsKit {
    public static <T> List<T> getImportData(File file, Class<T> clazz){
        List<T> result = new ArrayList<>(5);
        try {
            Field[] fields = clazz.getDeclaredFields();
            Map<String,Field> fieldMap = new HashMap<>(10);
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                CvsField cvs = field.getAnnotation(CvsField.class);
                if (cvs==null){
                    fieldMap.put(field.getName(),field);
                    continue;
                }
                fieldMap.put(cvs.headName(),field);
            }
            List<Map<String,String>> datas = getCvsData(new FileInputStream(file),fieldMap.keySet().stream().toArray(String[]::new));
            for (Map<String,String> bean:datas){
                Object object = clazz.newInstance();
                for (Map.Entry<String,Field> entry : fieldMap.entrySet()){
                    if (!bean.containsKey(entry.getKey())){
                        continue;
                    }
                    Field field = entry.getValue();
                    Object data = ImportExcelKit.getData(bean.get(entry.getKey()), field.getType());
                    if (data == null){
                        continue;
                    }
                    field.setAccessible(true);
                    field.set(object,data);
                }
                result.add((T) object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    private static List<Map<String, String>> getCvsData(FileInputStream inputStream,String ...field) {
        List<String> headers = Arrays.asList(field);
        List<Map<String, String>> result = new ArrayList<>();
        InputStreamReader isr = new InputStreamReader(inputStream);
        Reader reader = new BufferedReader(isr);
        try {
            CSVParser parser = CSVFormat.EXCEL.parse(reader);
            if (parser==null){
                return result;
            }
            List<CSVRecord> list = parser.getRecords();
            if (list == null){
                return result;
            }
            CSVRecord header = list.get(0);
            for(int i = 1;i<list.size();i++){
                CSVRecord record = list.get(i);
                if (record == null){
                    continue;
                }
                Map<String,String> data = new HashMap<>(header.size());
                for (int j = 0; j < header.size(); j++) {
                    if (!headers.contains(header.get(j))){
                        continue;
                    }
                    data.put(header.get(j),record.get(j));
                }
                result.add(data);

            }
        } catch (IOException e) {
            e.fillInStackTrace();
        }

        return result;
    }
}
